package com.leyou.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.server.BrandService;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BrandController {
    @Autowired
    private BrandService brandService;
    @GetMapping("/brand/page")
   public ResponseEntity<PageResult<Brand>>brandPageQuery(
           @RequestParam(value = "key",required = false) String key,
           @RequestParam(value = "page",defaultValue = "1") Integer page,
           @RequestParam(value = "rows",defaultValue = "10")  Integer rows,
           @RequestParam(value = "sortBy",required = false) String sortBy,
           @RequestParam(value = "desc",required = false) Boolean desc

    ){
        PageResult<Brand> pageResult = brandService.brandPageQuery(page, rows, sortBy, desc, key);
        return ResponseEntity.ok(pageResult);
    }
    /**
     *新增品牌
     */
   @PostMapping("/brand")
    public ResponseEntity<Void> saveBrand(
            Brand brand,@RequestParam("cids") List<Long>cids
   ){  brandService.brandSave(cids,brand);
        return ResponseEntity.status(HttpStatus.CREATED).build() ;
   }

    /**
     * 根据id查询品牌
     */
    @GetMapping("/brand/{id}")
    public Brand findById(
            @PathVariable("id") Long id
    ){
        Brand brand = brandService.findBrandById(id);
        return brand;
    }
    /**
     * 根据分类ID查询品牌
     */
    @GetMapping("/brand/of/category")

    public ResponseEntity<List<Brand>> findBrandsByCid(
            @RequestParam("id") Long id
    ){
      List<Brand> brandList=  brandService.findBrandsByCid(id);
      return ResponseEntity.ok(brandList);
    }
    @GetMapping("/brand/list")
    public ResponseEntity<List<Brand>> findBrandsByIds(@RequestParam("ids") List<Long> ids){
        List<Brand> brands = brandService.findBrandsByIds(ids);
        return ResponseEntity.ok(brands);
    }

}
